package com.runchen.broadcastdetection.mvp.login;

import com.blankj.utilcode.util.StringUtils;
import com.ruochen.common.base.ApiCallback;
import com.ruochen.common.base.BasePresenter;
import com.ruochen.common.entity.UserInfo;

/**
 * @Description 登录 MVP Presenter
 * @Auth RuoChen
 * @Date 04/04/2021, 14:20
 **/
public class LoginPresenter extends BasePresenter<LoginCovenant.MvpView, LoginCovenant.MvpStores>
        implements LoginCovenant.Presenter {

    public LoginPresenter(LoginCovenant.MvpView view) {
        attachView(view);
    }


    @Override
    public void login() {
        if (StringUtils.isTrimEmpty(mvpView.getAccountText())) {
            mvpView.showToast("请输入账号");
            return;
        }
        if (StringUtils.isTrimEmpty(mvpView.getPasswordText())) {
            mvpView.showToast("请输入密码");
            return;
        }
        if (mvpView.getPasswordText().length() < 6) {
            mvpView.showToast("请输入6到16位密码");
            return;
        }
        if (StringUtils.isTrimEmpty(mvpView.getCaptchaText())) {
            mvpView.showToast("请输入验证码");
            return;
        }
        if (StringUtils.isTrimEmpty(mvpView.getUUIDText())) {
            mvpView.showToast("验证码有误");
            return;
        }
        mvpView.showLoading("正字登录");
        addSubscription(appStores.login(mvpView.getAccountText(),
                mvpView.getPasswordText(),
                mvpView.getCaptchaText(),
                mvpView.getUUIDText()),
                new ApiCallback<UserInfo>(mvpView) {
                    @Override
                    public void onSuccess(UserInfo model) {
                        mvpView.hide();
                        mvpView.onLoginSuccess(model);
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                        mvpView.hide();
                        mvpView.onLoginFailure(code, msg);
                    }
                });
    }
}